package online.yysh.charging;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.RechargeRecord;
import online.yysh.charging.bean.response.GetRechargeRecordResponse;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 充值记录
 */
public class RechargeRecordActivity extends BaseActivity {

    private RecyclerView rv;
    private TextView tv_cancel,tv_title;
    private MyAdapter adapter;
    private List<RechargeRecord> rechargeRecordList = new ArrayList<>();

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 200:
                    adapter.notifyDataSetChanged();
                    break;
                case 201:
                    showToast("未查询到充值记录");
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rv = findViewById(R.id.rv_record);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(R.string.recharge_history);
    }

    @Override
    protected void initData() {
        getRechargeRecord();
        adapter = new MyAdapter();
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
    }

    /**
     * 获取充值记录
     */
    private void getRechargeRecord() {
        HttpUtil.getInstance().getRechargeRecord(openid, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("查询充值记录失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                if(result.contains("ok")){
                    GetRechargeRecordResponse getRechargeRecordResponse = new Gson().fromJson(result, GetRechargeRecordResponse.class);
                    if ("ok".equals(getRechargeRecordResponse.getStatus())) {
                        rechargeRecordList = getRechargeRecordResponse.getData();
                        handler.sendEmptyMessage(200);
                    }else{
                        handler.sendEmptyMessage(201);
                    }
                }else{
                    handler.sendEmptyMessage(201);
                }
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        @NonNull
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recharge_record, parent, false);
            return new MyAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
            RechargeRecord record = rechargeRecordList.get(position);
            holder.tv_no.setText("订单号："+record.getOrderno());
            String createTime = record.getCreatetime();
            holder.tv_time.setText("充值时间："+createTime.replace("T"," ").substring(0,createTime.length()-4));
            holder.tv_money.setText("账户余额(元)："+record.getTotal_money());
            holder.tv_recharge_money.setText("充值金额(元)："+record.getMoney());
        }

        @Override
        public int getItemCount() {
            return rechargeRecordList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv_no,tv_time,tv_money,tv_recharge_money;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_no = itemView.findViewById(R.id.tv_no);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_money = itemView.findViewById(R.id.tv_money);
                tv_recharge_money = itemView.findViewById(R.id.tv_recharge_money);
            }
        }
    }
}
